import 'dart:async';

import 'package:auto_route/auto_route.dart';
import 'package:distribution_mall_flutter/assets.dart';
import 'package:distribution_mall_flutter/constant/constant.dart';
import 'package:distribution_mall_flutter/provider/user/login.dart';
import 'package:distribution_mall_flutter/utils/common_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

@RoutePage()
class DeleteAccountDeleteSuccessPage extends ConsumerStatefulWidget {
  const DeleteAccountDeleteSuccessPage({super.key});

  @override
  ConsumerState createState() => _DeleteAccountDeleteSuccessPageState();
}

class _DeleteAccountDeleteSuccessPageState
    extends ConsumerState<DeleteAccountDeleteSuccessPage> {
  Timer? timer;
  int countDown = 5;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Account Deletion'),
        leading: CommonBtnWidget.arrowBack(),
      ),
      body: Container(
        width: double.infinity,
        child: Column(
          children: [
            Container(
              margin: const EdgeInsets.only(top: 97, bottom: 24),
              child: Image.asset(
                Assets.assets_icons_delete_account_delete_success_png,
                width: 80,
                height: 80,
              ),
            ),
            const Text(
              'THANK YOU FOR YOUR COMPANY',
              style: TextStyle(fontSize: 14, color: Color(0xFF242424)),
            ),
            Container(
              width: double.infinity,
              height: 48,
              margin: EdgeInsets.only(top: 80, left: 24, right: 24),
              child: ElevatedButton(
                onPressed: goHome,
                child: Text(
                  'GO HOME',
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 24),
              child: RichText(
                  text: TextSpan(children: [
                TextSpan(text: 'Go home in '),
                TextSpan(
                    text: countDown.toString(),
                    style: TextStyle(color: Color(0xFF5483BF))),
                TextSpan(text: ' s'),
              ], style: TextStyle(fontSize: 14, color: Color(0xFF868D96)))),
            )
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    timer = Timer.periodic(Duration(seconds: 1), (_) {
      countDown--;
      if (countDown <= 0) {
        goHome();
      } else {
        setState(() {});
      }
    });
  }

  goHome() {
    ref.read(loginStateProvider.notifier).outLogin(go2login: false);
    appRouter.popUntilRoot();
  }

  @override
  void dispose() {
    timer?.cancel();
    timer = null;
    super.dispose();
  }
}
